package com.alphaking.pojo.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SearchCondition implements Serializable{

	private Integer pageNum;
	private Integer size;
	private List<SearchBean> searchBeans;
	@Override
	public String toString() {
		return "SearchCondition [pageNum=" + pageNum + ", size=" + size
				+ ", searchBeans=" + searchBeans + "]";
	}
	public Integer getPageNum() {
		return pageNum;
	}
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
	public Integer getSize() {
		return size;
	}
	public void setSize(Integer size) {
		this.size = size;
	}
	public List<SearchBean> getSearchBeans() {
		return searchBeans;
	}
	public void setSearchBeans(List<SearchBean> searchBeans) {
		this.searchBeans = searchBeans;
	}
	public SearchCondition(Integer pageNum, Integer size,
			List<SearchBean> searchBeans) {
		super();
		this.pageNum = pageNum;
		this.size = size;
		this.searchBeans = searchBeans;
	}
	
	public SearchCondition(){
		
	}
	
	public final static String SPILT="-~-~-";
	
	public static String getHql(Class<?> clazz,SearchCondition searchCondition) throws NoSuchFieldException, SecurityException{
		
		List<SearchBean> sortBeans = new ArrayList<SearchBean>();
		List<SearchBean> searchBeans = searchCondition.getSearchBeans();
		
		//拼装hql
		String hql = "from "+clazz.getSimpleName()+" where 1=1";
		
		for(SearchBean searchBean : searchBeans){
			if(searchBean!=null && searchBean.getOperator()!=null && !searchBean.getOperator().equals("") && searchBean.getKey()!=null && !searchBean.getKey().equals("") && searchBean.getValue()!=null && !searchBean.getValue().equals("")){
				String operator = searchBean.getOperator();
				String key = searchBean.getKey();
				String value = searchBean.getValue();
				Integer priority = searchBean.getPriority();
				
				String condition = "";
				switch(operator){
				case SearchBean.OPERATOR_IN :
					String[] splitArray = value.split(SPILT);
					if(splitArray.length==1){
						condition = " and "+key+"=?";
						hql = hql + condition;
					}else{
						condition = " and "+key+" in (?";
						for(int j=1;j<splitArray.length;j++){
							condition = condition + ",?";
						}
						hql = hql + condition + ")";
					}
					break;
				case SearchBean.OPERATOR_LIKE :
					condition = " and "+key+" like ?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_SORT :
					if(priority!=null){
						sortBeans.add(searchBean);
					}
					break;
				case SearchBean.OPERATOR_IS_NULL:
					condition = " and "+key+" is null";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_EQ :
					condition = " and "+key+"=?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_NE :
					condition = " and "+key+"!=?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_LT :
					condition = " and "+key+"<?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_GT :
					condition = " and "+key+">?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_LE :
					condition = " and "+key+"<=?";
					hql = hql + condition;
					break;
				case SearchBean.OPERATOR_GE :
					condition = " and "+key+">=?";
					hql = hql + condition;
					break;
				default :
					break;
				}
			}
		}
		
		if(sortBeans!=null && sortBeans.size()>0){
			List<SearchBean> sortedBeans = new ArrayList<SearchBean>(sortBeans.size());
			for(SearchBean searchBean : sortBeans){
				sortedBeans.add(new SearchBean());
			}
			
			for(SearchBean searchBean : sortBeans){
				sortedBeans.set(searchBean.getPriority(),searchBean);
			}
			
			for (int i = 0; i < sortedBeans.size(); i++) {
				if (sortedBeans.get(i) != null) {
					String condition = null;
					if (i == 0) {
						condition = " order by " + sortedBeans.get(i).getKey()
								+ " " + sortedBeans.get(i).getValue();
					} else {
						condition = "," + sortedBeans.get(i).getKey() + " "
								+ sortedBeans.get(i).getValue();
					}
					hql = hql + condition;
				}
			}

		}
		
		return hql;
	}
	
	
	/**
	 * 对SearchCondition进行一些数据的初始化
	 * @param searchCondition
	 * @return
	 */
	public static SearchCondition initSearchCondition(SearchCondition searchCondition){
		if(searchCondition==null){
			searchCondition = new SearchCondition();
			searchCondition.setPageNum(1);
			searchCondition.setSize(20);
		}
		
		if(searchCondition.getSearchBeans()==null){
			searchCondition.setSearchBeans(new ArrayList<SearchBean>());
		}
		
		if(searchCondition.getPageNum()==null || searchCondition.getPageNum()<1){
			searchCondition.setPageNum(1);
		}
		
		if(searchCondition.getSize()==null || searchCondition.getSize()<=0){
			searchCondition.setSize(20);
		}
		
		return searchCondition;
	}
	
	/**
	 * 传入基础参数构建searchBean，为searchCondition添加查询数据
	 * @param searchCondition
	 * @param key
	 * @param operator
	 * @param value
	 * @param priority
	 * @return
	 */
	public static SearchCondition addSearchBeanByMap(SearchCondition searchCondition,String key,String operator,String value,Integer priority){
		if(searchCondition==null){
			searchCondition = initSearchCondition(searchCondition);
		}
		if(searchCondition.searchBeans==null){
			searchCondition.searchBeans = new ArrayList<SearchBean>();
		}
		searchCondition.searchBeans.add(new SearchBean(key,operator,value,priority));
		return searchCondition;
	}
	
	/**
	 * 直接传入searchBean，为searchCondition添加查询数据
	 * @param searchCondition
	 * @param searchBean
	 * @return
	 */
	public static SearchCondition addSearchBeanByPojo(SearchCondition searchCondition,SearchBean searchBean){
		if(searchCondition==null){
			searchCondition = initSearchCondition(searchCondition);
		}
		if(searchCondition.searchBeans==null){
			searchCondition.searchBeans = new ArrayList<SearchBean>();
		}
		searchCondition.searchBeans.add(searchBean);
		return searchCondition;
	}
	
	/**
	 * 直接传入searchBean数组
	 * @param searchCondition
	 * @param searchBeans
	 * @return
	 */
	public static SearchCondition addSearchBeans(SearchCondition searchCondition,List<SearchBean> searchBeans){
		if(searchCondition==null){
			searchCondition = initSearchCondition(searchCondition);
		}
		searchCondition.searchBeans = searchBeans;
		return searchCondition;
	}
}
